home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / BTREE101.ZIP / BTCLOSE.C next >
Text File  |  1990-06-20  |  2KB  |  77 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btclose.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. /*#include <string.h>*/
  9.  
  10. /* local headers */
  11. #include "btree_.h"
  12.  
  13. /*man---------------------------------------------------------------------------
  14. NAME
  15.      btclose - close btree
  16.  
  17. SYNOPSIS
  18.      #include <btree.h>
  19.  
  20.      int btclose(btp)
  21.      btree_t *btp;
  22.  
  23. DESCRIPTION
  24.      The btclose function causes any buffered data for the named btree
  25.      to be written out, the file unlocked, and the btree to be closed.
  26.  
  27.      btclose will fail if one or more of the following is true:
  28.  
  29.      [EINVAL]       btp is not a valid btree pointer.
  30.      [BTENOPEN]     btp is not open.
  31.  
  32. SEE ALSO
  33.      btcreate, btlock, btopen, btsync.
  34.  
  35. DIAGNOSTICS
  36.      Upon successful completion, a value of 0 is returned.  Otherwise,
  37.      a value of -1 is returned, and errno set to indicate the error.
  38.  
  39. ------------------------------------------------------------------------------*/
  40. int btclose(btp)
  41. btree_t *btp;
  42. {
  43.     /* validate arguments */
  44.     if (!bt_valid(btp)) {
  45.         errno = EINVAL;
  46.         return -1;
  47.     }
  48.  
  49.     /* check if not open */
  50.     if (!(btp->flags & BTOPEN)) {
  51.         errno = BTENOPEN;
  52.         return -1;
  53.     }
  54.  
  55.     /* synchronize file with buffers and unlock file */
  56.     if (btlock(btp, BT_UNLCK) == -1) {
  57.         BTEPRINT;
  58.         return -1;
  59.     }
  60.  
  61.     /* free memory allocated for btree */
  62.     bt_free(btp);
  63.  
  64.     /* close btree file */
  65.     if (bclose(btp->bp) == -1) {
  66.         BTEPRINT;
  67.         return -1;
  68.     }
  69.  
  70.     /* scrub slot in btb table then free it */
  71.     memset(btp, 0, sizeof(*btb));
  72.     btp->flags = 0;
  73.  
  74.     errno = 0;
  75.     return 0;
  76. }
  77.